﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECCIT.Data;

namespace OgilvyOne.UKNow.DAL
{
    public class MemberCheckInDAL
    {
        private readonly string dbName = "UKNOW_EVENTS_CHECKIN";
        private readonly string UserId = "MEMBER_ID";
        private readonly string EventId = "COL_EVENTS_ID";
        private readonly string LangID = "COL_LANG_ID";
        private readonly string CheckDate = "COL_CHECKIN_DATE";

        /// <summary>
        /// 判断会员是否已签到
        /// </summary>
        /// <param name="uid">会员ID</param>
        /// <param name="eventid">活动ID</param>
        /// <returns></returns>
        public bool IsCheckIn(int uid, int eventid)
        {
            /*
             * 代码修改：
             * 修改日期：2012-04-25
             * 修改人员：刘扬
             * 修改原因：原先每个活动当日只可签到一次，现修改为每个活动只能签到一次
             * */
            //object o= DBHelper.ExecuteScalar(String.Format("select count(*) from {0} where {1}={2} and {3}={4} and {5}='{6}'",dbName,UserId,uid,EventId,eventid,CheckDate,DateTime.Now.ToString("yyyy-MM-dd")));
            object o = DBHelper.ExecuteScalar(String.Format("select count(*) from {0} where {1}={2} and {3}={4}", dbName, UserId, uid, EventId, eventid));

            int cNum = int.Parse(o.ToString());

            return cNum == 0 ? false : true;
        }

        /// <summary>
        /// 会员签到
        /// </summary>
        /// <param name="uid">会员ID</param>
        /// <param name="eventid">活动Id</param>
        /// <returns>签到是否成功</returns>
        public bool CheckIn(int uid, int eventid)
        {
            int res = DBHelper.ExecuteNonQuery(String.Format("insert into {0} values ({1},{2},{3},'{4}')", dbName, 2052, uid, eventid, DateTime.Now.ToString("yyyy-MM-dd")));
            return res > 0 ? true : false;
        }

        /// <summary>
        /// 根据活动ID获取已经签到数量
        /// </summary>
        /// <param name="eventid">活动ID</param>
        /// <returns>签到数量</returns>
        public int GetCheckInNum(int eventid)
        {
            object o = DBHelper.ExecuteScalar(String.Format("select count({1}) from {0} where {1}={2} ", dbName, EventId, eventid.ToString()));
            int cNum = int.Parse(o.ToString());
            return cNum;
        }
    }
}
